<template>
  <div>
    <!-- 利润表查询 -->
    <TopFilter ref="form" :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhIncomeReportSubmit"
        :isToperInfo="false"
        :isColumnFilter="false"
        :isSelectColumn="false"
        :columns="columns"
        :dataSource="checkList"
        :onColumnsChange="columns => (this.columns = columns)"
      >
      </FilterTable>
    </Spin>

    <!-- 经销商查询 -->
    <BaseDialog :visible.sync="isDealer" title="经销商查询" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <OwnerSearch :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>
    <!-- 详情展示 -->
    <Drawer :visible.sync="actionsOwner.visible" :title="actionsOwner.title" width="80%" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DetailView :formData="this.tableList" :viewStatus="this.passStatus" :requestId="this.reportId" :year="this.year" :month="this.month" @close="closeSetting" @refrish="refrishList"/>
    </Drawer>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils/index';
import { queryList, exportExcel, submitData } from '@/api/omFinance/CRReportManage/IncomeReportSubmit';
import OwnerSearch from '../OwnerSearch';
import DetailView from './detailView';
export default {
  name: 'IncomeReportQuery',
  mixins: [dictionary],

  components: {
    OwnerSearch,
    DetailView,
  },
  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '明细' };
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      formList: [],
      isDealer: false,
      isDisabled: true,
      exportLoading: false,
      formData: {},
      tableList: [],
      reportId: '',
      checkList: [],
      year: moment().format('YYYY'),
      month: moment().format('MM'),
      dealerCode: '',
      statusCode:'',
      actionsOwner: { visible: false, title: '', type: '' },
      passStatus:'',
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
  //刷新
  refrishList(){
    this.getList(this.year, this.month, this.dealerCode,this.statusCode);
  },
    //展示详情
    openHandler(data){

     this.reportId = data.id; // 导出传id
     this.passStatus = data.profitStatus;
     console.log(this.passStatus);
     this.tableList = [
            {
              column1: '编制单位:',
              column2: data.dealerCode,
              column3: data.dealerName,
              column4: '单位：元'
            },
            {
              column1: '项    目',
              column2: '行次',
              column3: '本月数',
              column4: '本年累计数'
            },
            {
              column1: '一、主营业务收入',
              column2: '1',
              column3: data.byzyywsr,
              column4: data.bnzyywsr
            },
            {
              column1: '减：主营业务成本',
              column2: '2',
              column3: data.byzyywcb,
              column4: data.bnzyywcb
            },
            {
              column1: '主营业务税金及附加',
              column2: '5',
              column3: data.byzyywsjjfj,
              column4: data.bnzyywsjjfj
            },
            {
              column1: '二、主营业务利润（亏损以“－”号填列）',
              column2: '10=1-2-5',
              column3: data.byzyywlr,
              column4: data.bnzyywlr
            },
            {
              column1: '加：其他业务利润（亏损以“－”号填列）',
              column2: '11',
              column3: data.byqtywlr,
              column4: data.bnqtywlr
            },
            {
              column1: '减： 营业费用',
              column2: '13',
              column3: data.byyyfy,
              column4: data.bnyyfy
            },
            {
              column1: '管理费用',
              column2: '14',
              column3: data.byglfy,
              column4: data.bnglfy
            },
            {
              column1: '财务费用',
              column2: '15',
              column3: data.bycwfy,
              column4: data.bncwfy
            },
            {
              column1: '三、营业利润（亏损以“－”号填列）',
              column2: '18=10+11-13-14-15',
              column3: data.byyylr,
              column4: data.bnyylr
            },
            {
              column1: '加：投资收益（损失以“－”号填列）',
              column2: '19',
              column3: data.bytzsy,
              column4: data.bntzsy
            },
            {
              column1: '补贴收入',
              column2: '22',
              column3: data.bybtsr,
              column4: data.bnbtsr
            },
            {
              column1: '营业外收入',
              column2: '23',
              column3: data.byyywsr,
              column4: data.bnyywsr
            },
            {
              column1: '减：营业外支出',
              column2: '25',
              column3: data.byyywzc,
              column4: data.bnyywzc
            },
            {
              column1: '四、利润总额（亏损总额以“－”号填列）',
              column2: '27=18+19+22+23-25',
              column3: data.bylrze,
              column4: data.bnlrze
            },
            {
              column1: '减：所得税',
              column2: '28',
              column3: data.bysds,
              column4: data.bnsds
            },
            {
              column1: '五、净利润（净亏损以“－”号填列）',
              column2: '30=27-28',
              column3: data.byjlr,
              column4: data.bnjlr
            }
          ];
        
        this.actionsOwner = { visible: true, title: '编辑', type: 'show' };

    },
    closeSetting() {
        this.actionsOwner.visible = false;
    },
    seacherFun() {
      this.isDealer = true;
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    changeHandle(params) {
      this.year = params.yearMonth.split('-')[0];
      this.month = params.yearMonth.split('-')[1];
      this.dealerCode = params.ownerCode;
      this.statusCode = params.profitStatus;
      console.log(params);
      this.getList(this.year, this.month, this.dealerCode,this.statusCode);
    },
   
    emitCloseHandler(val, data) {
      this.isDealer = val;
      if (data) {
        let dealerCodes = [];
        let dealerNames = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
          dealerNames.push(x.dealerName);
        });
        this.topFilterList.find(x => x.fieldName == 'ownerCode').initialValue = dealerCodes.toString();
        this.topFilterList.find(x => x.fieldName == 'dealerName').initialValue = dealerNames.toString();
      }
    },

    // 查询列表
    async getList(Year = '', Month = '', dealerCode = '',statusCode = '') {
      console.log(this.year,this.month);
      this.exportLoading = true;
      const res = await queryList({ reportYear: Year, reportMonth: Month, dealerCode: dealerCode ,profitStatus:statusCode});
      this.reportId = '';
      this.tableList = [];
      if (res.resultCode == 200) {
        if (res.data) {
          this.isDisabled = false;
          const data = res.data; // 表格
          this.checkList = res.data;
        } else {
          this.isDisabled = true;
          this.tableList = [];
          // 驳回意见清空
          this.formList[0].rules[0].required = false;
          this.formList[0].initialValue = '';
        }
      }
      this.exportLoading = false;
    },

    createdFrom() {
      return [
        {
          type: 'DATE',
          label: '月度',
          fieldName: 'yearMonth',
          dateType: 'month',
          initialValue: moment().format('YYYY-MM'),
          placeholder: '请选择'
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'ownerCode',
          placeholder: '请选择',
          rules: [{ required: false, message: '请选择', trigger: 'blur' }],
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        // {
        //   type: 'INPUT',
        //   label: '经销商名称',
        //   fieldName: 'dealerName',
        //   placeholder: '',
        //   disabled: true
        // },
        {
          type: 'SELECT',
          label: '审核状态',
          fieldName: 'profitStatus',
          placeholder: '',
          itemList: this.createDictList('6077')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 70,
          render: props => {
            return (
              <div style="text-align:center">
                <el-button size="mini" type="text" onClick={() => this.openHandler(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
       {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报日期',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '状态',
          dataIndex: 'profitStatus',
          sorter: true,
          filter: true,
          filterType: 'input',
          filterItems: this.createDictList('6077'),
          render: props => {
            return <span>{this.createDictText(props.row.profitStatus, '6077')}</span>;
          }
        },
        {
          title: '驳回原因',
          dataIndex: 'checkDesc',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
